<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button
            class="button"
            type="primary"
            size="mini"
            @click="button"
          >+ 新增角色</el-button>
        </el-tab-pane>
        <el-table border :data="list">
          <el-table-column label="序号" width="80px" type="index">
            <template v-slot="{ $index }">
              <!-- 序号真实处理 -->
              <div>{{ (page.page - 1) * page.pagesize + $index + 1 }}</div>
            </template>
          </el-table-column>
          <el-table-column label="角色名" prop="name" sortable />
          <el-table-column label="描述" prop="description" sortable />
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <div>
                <el-button
                  type="text"
                  @click="permission(row)"
                >分配权限</el-button>
                <el-button type="text" @click="lock(row)">修改</el-button>
                <el-button type="text" @click="del(row)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-tab-pane label="公司信息" />
      </el-tabs>
      <div class="page">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 10, 100, 200, 300, 400]"
          :page-size="page.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 弹框 -->
    <!-- <Dialog :is-show="show" @input="show = $event" /> -->
    <Dialog ref="dialog" v-model="show" @getData="getData" />
    <!-- 权限弹框组件 -->
    <!-- <SetPermission :is-show="isShow" @setIsShow="isShow = $event" /> -->
    <SetPermission ref="setPermission" v-model="isShow" />
  </div>
</template>

<script>
import { sysRole, sysRoleDelete } from '@/api/setting.js'
import Cookies from 'js-cookie'
import Dialog from './components/dialog.vue'
import SetPermission from './components/setPermission.vue'
export default {
  components: {
    Dialog,
    SetPermission
  },
  data() {
    return {
      list: [],
      show: false,
      isShow: false, // 是否显示权限弹框
      page: {
        page: 1,
        pagesize: +Cookies.get('settingSize') || 2, // 获取页容量数据
        total: 100
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysRole(this.page)
      // 保存角色列表数据
      this.list = res.data.rows
      this.page.total = res.data.total
      console.log(res)
    },
    handleSizeChange(size) {
      console.log(size)
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
      // 通过浏览器存储相应的页容量
      Cookies.set('settingSize', size)
      // console.log('handleSizeChange')
    },
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
      // console.log('handleCurrentChange')
    },
    // 新增点击
    button() {
      // 显示弹框
      this.show = true
      this.$refs.dialog.mode = 'dialog'
    },
    // 修改
    lock(row) {
      // 显示弹框
      this.show = true
      // 数据拷贝回显
      this.$refs.dialog.form = JSON.parse(JSON.stringify(row))
      this.$refs.dialog.mode = 'lock'
    },
    // 删除
    del(row) {
      console.log(row)
      this.$confirm('确定要删除吗', '温馨提示')
        .then(async() => {
          await sysRoleDelete(row.id)
          // 提示
          this.$message.success('删除成功')
          // 刷新页面数据
          this.getData()
        })
        .catch(() => {})
    },
    // 分配权限点击
    permission(row) {
      this.isShow = true
      this.$refs.setPermission.getRuleInfo(row.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 20px;
  .button {
    margin: 20px 0;
  }
  .page {
    text-align: right;
    margin-top: 20px;
  }
}
</style>
